package com.ruoyi.huanghefd.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.huanghefd.domain.ApplysheetPoweroffon;
import com.ruoyi.huanghefd.domain.ContactsheetRj;
import com.ruoyi.huanghefd.domain.Makesheet;
import com.ruoyi.huanghefd.service.ContactsheetRjService;
import com.ruoyi.huanghefd.mapper.ContactsheetRjMapper;
import com.ruoyi.huanghefd.utils.PageBean;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 *
 */
@Service
public class ContactsheetRjServiceImpl extends ServiceImpl<ContactsheetRjMapper, ContactsheetRj>
    implements ContactsheetRjService{

    private static final Logger log = LoggerFactory.getLogger(ContactsheetRjServiceImpl.class);

    @Resource
    ContactsheetRjMapper contactsheetRjMapper;

    @Override
    public AjaxResult addContactsheetRj(ContactsheetRj contactsheetRj) {
        int result = 0;
        try {
            // 联系单*（热机检修工作停电、送电） 主键
            String strCnId = UUID.randomUUID().toString().replace("-", "");

            String strCnCode =this.getGenerateOrder(contactsheetRj.getCnType());
            log.info("生成联系单*（热机检修工作停电、送电）票号：" + strCnCode);
            contactsheetRj.setCnCode(strCnCode);
            contactsheetRj.setCnId(strCnId);

            //插入 联系单*（热机检修工作停电、送电） 主表
            log.info("新增联系单*（热机检修工作停电、送电）-开始>>" + contactsheetRj);
            result = contactsheetRjMapper.insertAll(contactsheetRj);
            log.info("新增联系单*（热机检修工作停电、送电）-结束<<" + result);

            return AjaxResult.success(strCnCode);
        } catch (Exception e) {
            return AjaxResult.error("联系单*（热机检修工作停电、送电）新增失败", e);
        }
    }

    @Override
    public AjaxResult delContactsheetRj(String strCnCode) {
        int result = 0;
        try {
            log.info("删除联系单*（热机检修工作停电、送电）-开始>>");
            result = contactsheetRjMapper.delByCnCode(strCnCode);
            log.info("删除联系单*（热机检修工作停电、送电）-结束<<"+result);

            return AjaxResult.success(strCnCode);
        } catch (Exception e) {
            return AjaxResult.error("联系单*（热机检修工作停电、送电）删除失败", e);
        }
    }

    @Override
    public AjaxResult uptContactsheetRj(ContactsheetRj contactsheetRj) {
        int result = 0;
        try {
            log.info("修改联系单*（热机检修工作停电、送电）-开始>>");
            result = contactsheetRjMapper.updateByCnId(contactsheetRj);
            log.info("修改联系单*（热机检修工作停电、送电）-结束<<"+result);

            return AjaxResult.success(contactsheetRj.getCnCode());
        } catch (Exception e) {
            return AjaxResult.error("修改联系单*（热机检修工作停电、送电）修改失败", e);
        }
    }

    @Override
    public AjaxResult queryContactsheetRjs(String strCnCode, String strCnState, String strUserId, String strMailType) {
        try {
            List<ContactsheetRj> contactsheetRjList = contactsheetRjMapper.queryContactsheetRjList(strCnCode,strCnState,strUserId,strMailType);
            if (CollectionUtils.isEmpty(contactsheetRjList)) {
                return AjaxResult.success("联系单*（热机检修工作停电、送电）查询无数据",null);
            }
            return AjaxResult.success("联系单*（热机检修工作停电、送电）询成功",contactsheetRjList);
        } catch (Exception e) {
            return AjaxResult.warn("联系单*（热机检修工作停电、送电）查询失败",e);
        }
    }

    @Override
    public PageBean queryContactsheetRjsByPage(String strCnCode, String strCnState, String strUserId, String strMailType, int pageNum, int pageSize) {
        PageBean pageBean = new PageBean();
        if (pageNum > 0 && pageSize > 0) {
            int totalRecord = contactsheetRjMapper.getContactsheetRjNum(strCnCode, strCnState, strUserId, strMailType);
            pageBean = new PageBean(pageNum, pageSize, totalRecord);

            //从该行开始拿数据
            int startIndex = (pageNum - 1) * pageSize;
            pageBean.setStartIndex(startIndex);
            pageBean.setCode(200);

            //数据集合
            List<ContactsheetRj> contactsheetRjList = contactsheetRjMapper.queryContactsheetRjListByPage(strCnCode, strCnState, strUserId, strMailType,startIndex, pageSize);
            pageBean.setList(contactsheetRjList);
            pageBean.setSuccessMsg("联系单*（热机检修工作停电、送电）查询成功！");
        } else {
            pageBean.setCode(500);
            pageBean.setFailMsg("联系单*（热机检修工作停电、送电）查询失败！");
        }
        return pageBean;
    }

    /**
     * @Author gnplum
     * @Description //联系单*（热机检修工作停电、送电）票号
     * @Date 14:56 2023/11/12
     * @param Type
     * @return java.lang.String
     **/
    private String getGenerateOrder(String Type) {

        log.info("获取票编号=" + Type);
        String strWsCode = "";
        int order = 0;
        try {

            order = contactsheetRjMapper.getCnCodeNum(Type);
            log.info("order=" + order);
            //DQ1202311110001
            strWsCode = Type + new SimpleDateFormat("yyyyMMdd").format(new Date()) + String.format("%03d", order);

            log.info("strWsCode=" + strWsCode);

        } catch (Exception e) {
            strWsCode = "";
            //e.printStackTrace();
            throw  e;
            //log.error(e.getCause()+e.getMessage());
        }
        return strWsCode;
    }
}




